import pasteboard from '@ohos.pasteboard';
import JsDataStore from './JsDataStore'
import JsLogger from './JsLogger'

export class JsPasteBoard {

    constructor() {
        let systemPasteboard = pasteboard.getSystemPasteboard();
        systemPasteboard.on('update', () => {
            JsDataStore.getQtNativeModule("QPA").pasteChanged();
        });
    }

    async hasClipboardText() : Promise<boolean> {
        let systemPasteboard = pasteboard.getSystemPasteboard();
        let result = await systemPasteboard.hasData()
        return result;
    }

    async clipboardText() : Promise<string> {
        let systemPasteboard = pasteboard.getSystemPasteboard();
        let pastedata = await systemPasteboard.getData();
        let result = pastedata.getPrimaryText();
        return result;
    }

    setClipboardText(text: string) : boolean {
        JsLogger.info('set clipboard text %{public}s', text);
        var pasteData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text);
        let systemPasteboard = pasteboard.getSystemPasteboard();
        systemPasteboard.setData(pasteData);
        return true;
    }
}
